package com.jjvu.news.controller;

import com.jjvu.news.dao.Sys_user;
import com.jjvu.news.service.UserService;
import com.jjvu.news.util.MagSend;
import com.jjvu.news.util.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Autowired
    private UserService userService;
    private  String s;
    @RequestMapping(value = "/getMsg/{input}",method = RequestMethod.GET)
    public String getMsg( @PathVariable("input") String input){
        s=ValidateCodeUtils.generateValidateCode(6)+"";
        MagSend.sendMSG(input,s);
        System.out.println("验证码"+s);
        return "admin/index";
    }
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "admin/index";
    }

    @RequestMapping("/login")
    public String toLogin(Sys_user sys_user,
                          HttpSession session, Model model) {
        Object s1= sys_user.getVerifyCode();
        System.out.println("用户=========>"+sys_user);
        if(!userService.findSys_UserByNameAndPassword(sys_user)) {
            System.out.println("man:"+sys_user.getVerifyCode()+",yzm:"+s);
            model.addAttribute("msg", "用户名或密码错误");
            return "admin/index";
        }else if(!(s1.equals(s))) {
            System.out.println("man:"+sys_user.getVerifyCode()+",yzm:"+s);
            model.addAttribute("msg", "验证码错误");
            return "admin/index";
        }else {
            System.out.println("man:"+sys_user.getVerifyCode()+",yzm:"+s);
            session.setAttribute("sys_user", sys_user);
            System.out.println(sys_user);
            return "admin/admin_index";
        }
    }
}
